<?php


class Member_Model_DbTable_Beuser_Row extends WSB_Db_Row
{

	/**
	 * @var WSB_Acl
	 */
	protected $_acl;

	public function init()
	{
		$this->_formConfigPath = WSB_MODULES_PATH . 'member/models/Beuser/form.config';
	}

	/**
	 * @return void
	 */
	public function preSave()
	{
		// Passwort
		if(empty($this->password)) {
			if(empty($this->id)) {
				$this->password = '';
			} else {
				$select = $this->_table->select();
				$select->where('id=?', $this->id);
				$row = $this->_table->fetchRow($select);
				$this->password = $row->password;
			}
		} else {
			$this->password = md5($this->password);
		}
	}

	/**
	 * @return string
	 */
	public function getTitle()
	{
		if(empty($this->firstname) && empty($this->name)) {
			return sprintf('%s', $this->username);
		} else {
			return sprintf('%s %s', $this->firstname, $this->name);
		}
	}

	/**
	 * @return boolean
	 */
	public function isAdmin()
	{
		return ($this->admin == 1);
	}

	/**
	 * Gib den Defaultcontroller des Users zurueck.
	 *
	 * @return string
	 */
	public function getDefaultController()
	{
		if(!empty($this->defaultcontroller)) {
			return $this->defaultcontroller;
		}
		return 'index';
	}

	/**
	 * Initialisiere ACL
	 *
	 */
	private function _initAcl()
	{
		// Access Control List
		$this->_acl = new WSB_Acl();
		$this->_acl->addRole(new Zend_Acl_Role(WSB_ACL_ROLE_ADMIN));
		$this->_acl->addRole(new Zend_Acl_Role(WSB_ACL_ROLE_STAFF));
		$this->_acl->add(new Zend_Acl_Resource('DEFAULT'));
		$this->_acl = $this->_buildAcl($this->_acl);
		//$this->_acl = $this->_buildDcl($this->_acl);
	}

	/**
	 * Gib die ACL zurueck.
	 *
	 * @return unknown
	 */
	public function getAcl()
	{
		if(is_null($this->_acl)) {
			$this->_initAcl();
		}
		return $this->_acl;
	}

	/**
	 * Hole aus der Tabelle Begroups die ACL-/DCL-Eintraege
	 *
	 * @param Zend_Acl $acl
	 * @return Zend_Acl
	 */
	private function _buildAcl(Zend_Acl $acl)
	{
		if($this->isAdmin()) {
			$acl->allow(WSB_ACL_ROLE_ADMIN);
		} else {
			$table = $this->getTableClass();
			// $this reaktivieren (Verbindung mit DB erstellen)
			$this->setTable(new $table());
			// @todo: findManyToManyRowset erweitern, sodass hidden und deleted mitberuecksichtig werden
			$begroups = $this->findManyToManyRowset('Member_Model_DbTable_Begroup', 'Member_Model_DbTable_Bemember');
			$begroups->unsetDeletedRows();
			$begroups->unsetHiddenRows();
			if($begroups instanceof Member_Model_DbTable_Begroup_Rowset) {
				foreach($begroups AS $begroup) {
					// Access control list
					$aclLines = WSB_Div::explode("\n", $begroup->acl);
					foreach($aclLines AS $aclEntry) {
						$aclEntry = trim($aclEntry);
						if(!empty($aclEntry)) {
							$acl->allow(WSB_ACL_ROLE_STAFF, 'DEFAULT', $aclEntry);
						}
					}
					// Denied control list
					$dclLines = WSB_Div::explode("\n", $begroup->dcl);
					foreach($dclLines AS $dclEntry) {
						$dclEntry = trim($dclEntry);
						if(!empty($dclEntry)) {
							$acl->deny(WSB_ACL_ROLE_STAFF, 'DEFAULT', $dclEntry);
						}
					}
    			}
			}
			$acl->allow(WSB_ACL_ROLE_STAFF, 'DEFAULT', 'default.index.index');
			$acl->allow(WSB_ACL_ROLE_STAFF, 'DEFAULT', 'index.index.index');
		}
		return $acl;
	}

}